/********************************
 *FIRE DEMO
 *By Dacre Denny
 *www.dacredenny.com
 *
 ********************************/

#include <windows.h>

/********************************
 *Engine Includes
 ********************************/
#include "../ExEngine/engine/memoryalloc.h"
#include "../ExEngine/engine/time.h"
#include "../ExEngine/input/dinput.h"
#include "../ExEngine/engine/engine.h"
#include "../ExEngine/engine/error.h"
#include "../ExEngine/engine/log.h"
#include "../ExEngine/render/d3d.h"
#include "../ExEngine/sound/dsound.h"


/********************************
 *Demo includes
 ********************************/ 
#include "scene.h"

/********************************
 *Demo globals
 ********************************/ 
CObject* pPlatform = NULL;
CFire* pFire = NULL;

HRESULT ShutdownDemo()
{
	//TODO:this
	return S_OK; 
}

HRESULT LoadDemo()
{
	CCamera::cameraData cameraSettings = {2.0f,100.0f,150.0f}; 
	CEngine::GetInstance()->GetCamera()->initalizeCamera(cameraSettings); 
	CEngine::GetInstance()->GetCamera()->setState(CCamera::FREEVIEW); 
	CEngine::GetInstance()->GetCamera()->setPosition(Vector3f(0,10,0)); 
	CEngine::GetInstance()->GetCamera()->setDirection(Vector3f(1,0,0));

	if(FAILED(CEngine::GetInstance()->CreateObject(string("Platform.mdl"), &pPlatform, MODEL_MESH)))
		throw EE_EXCEPTION("Failed to create platform model.");
	pPlatform->getModel()->setRenderFlags(RENDER_DEFAULT);

	EE_SAFEDELETE(pFire);
	pFire = EE_NEW CFire();
	if(FAILED(CEngine::GetInstance()->CreateObject(pFire)))
		throw EE_EXCEPTION("Failed to create fire object.");
	
	D3DTexture3D *pTexture = NULL;
	if(FAILED(CEngine::GetInstance()->GetRenderer()->CreateTexture(string("Effects\\Env_Cubemap.dds"), (Texture**)&pTexture)))
		throw EE_EXCEPTION("Failed to load demo cubemap");

	CEngine::GetInstance()->GetRenderer()->SetEnvironmentCubemap(pTexture);
	return S_OK; 
}

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	hPrevInstance = NULL;
	lpCmdLine = NULL;
	nShowCmd = 0;

	try
	{
		if(!CLog::Open())
			throw EE_EXCEPTION("Could not Create log file\n"); 
		
		CEngine::GetInstance()->SetState(true, CEngine::REALTIME); 
		CEngine::GetInstance()->SetContentDirectory(string("FireDemo")); 

		if(FAILED(CEngine::GetInstance()->GetWindow()->Create(hInstance, string("The Discovery - dacredenny.com"))))
			throw EE_EXCEPTION("Could not Create window\n"); 

		//SetState engine features that are required
		DeviceSettings settings = { false, 1024, 768, 32, 32 }; 
		if(FAILED(CEngine::GetInstance()->GetRenderer()->Initailize(settings)))
			throw EE_EXCEPTION("Could not initalize render device\n"); 
		
		if(FAILED(CEngine::GetInstance()->GetInput()->Initailize()))
			throw EE_EXCEPTION("Could not initalize input device\n"); 
			
		if(FAILED(CEngine::GetInstance()->GetSound()->Initailize()))
			throw EE_EXCEPTION("Could not initalize sound device\n"); 
			
		if(FAILED(CEngine::GetInstance()->GetTime()->Initailize()))
			throw EE_EXCEPTION("Could not initalize timer device\n"); 
		
		CEngine::GetInstance()->SetAppInitializeFunc(&LoadDemo); 
		CEngine::GetInstance()->SetAppThinkFunc(&UpdateDemo); 
		CEngine::GetInstance()->SetAppShutdownFunc(&ShutdownDemo);

		CEngine::GetInstance()->GetTime()->StartThread(); 
		CEngine::GetInstance()->StartEngineLoop(); 

		if(FAILED(CEngine::GetInstance()->Release()))
			throw EE_EXCEPTION("Could not Release engine\n"); 
		
		CEngine::GetInstance()->GetWindow()->Release(); 
		
		CEngine::DeleteInstance(); 
		CLog::Close(); 

		return 0; 
	}
	catch(CError::CErrorData error)
	{
		CError::CError(error, ERRORREPORT_LOG);
		CLog::Close(); 
		return -1; 
	}
}